import Cookies from 'js-cookie'

const USER_KEY = 'user_key'
const TOKEN_KEY = 'token_key'

export function getToken() {
  const token = Cookies.get(TOKEN_KEY)
  return token
}

export function setToken(token) {
  return Cookies.set(TOKEN_KEY, token)
}
export function removeToken() {
  return Cookies.remove(TOKEN_KEY)
}

export function getUser() {
  let user = Cookies.get(USER_KEY)
  if (user) {
    user = JSON.parse(user)
  }
  return user
}

export function setUser(user) {
  return Cookies.set(USER_KEY, JSON.stringify(user))
}

export function removeUser() {
  return Cookies.remove(USER_KEY)
}

export function getPermission() {
  const user = getUser()
  let permissions = []
  if (user) {
    permissions = user.perms
  }
  return permissions
}
